package com.itboye.mengwu.main;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.logincontroller.LoginController;
import com.itboye.mengwu.app.ui.mymeng.personsettings.ActivitySiXinXiangQing;
import com.itboye.mengwu.app.ui.roof.ActivityHomePageSearch;
import com.itboye.mengwu.app.ui.roof.HomPageItemFragment;
import com.itboye.mengwu.app.ui.roof.HomePageItemFragmentTypeTwo;
import com.itboye.mengwu.app.ui.roof.TodaySpecialOfferItemFragment;
import com.itboye.mengwu.data.model.HomePageModel;
import com.itboye.mengwu.data.model.LunBoModel;
import com.itboye.mengwu.data.presenter.LunBoPresenter;
import com.itboye.mengwu.widget.FixGridLayout;
import com.itboye.mengwu.widget.PagerSlidingTabStrip;

/***
 * 屋顶Fragment
 * 
 * @author dsawe
 * 
 */
public class RoofFragment extends BaseFragment implements Observer {
	String[] titles = { "上新", "童鞋", "童装", "用品", "玩具", "女装" };
	String[] biaoqian_1 = { "亲子", "女装", "童鞋", "婴童用品", "美食", "居家" };
	String[] biaoqian_2 = { "亲子2", "女装2", "童鞋2", "婴童用品2", "美食2", "居家2" };
	ViewPager pager;
	PagerSlidingTabStrip tabs;
	boolean isShow = false;
	TextView txt_more;
	private ImageView img_other, back;
	LunBoPresenter boPresenter;
	protected PopupWindow popupWindow;
	static boolean isreturn;
	HomePageModel arrayBanner;
	ArrayList<HomePageModel> arrayList = new ArrayList<HomePageModel>();

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.fragment_roof;
	}

	@Override
	public void onMyActivityCreated() {
		// showProgressDialog("数据加载中，请稍后", true);
		// back.setBackgroundResource(R.drawable.acy);
		// img_other.setBackgroundResource(R.drawable.news);

		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						ActivityHomePageSearch.class);

			}
		});
		img_other.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				LoginController.QuanMessge(getActivity(), null);
			}
		});
		txt_more.setOnClickListener(new OnClickListener() {

			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				View view = LayoutInflater.from(getActivity()).inflate(
						R.layout.popupwindow_more, null);
				popupWindow = new PopupWindow(getActivity().getWindowManager()
						.getDefaultDisplay().getWidth(), 200);
				popupWindow.setBackgroundDrawable(new BitmapDrawable(
						BitmapFactory.decodeResource(getResources(),
								R.drawable.bg_category_normal)));
				// popupWindow.setBackgroundDrawable(new BitmapDrawable());
				popupWindow.setContentView(view);
				FixGridLayout biaoqian_1 = (FixGridLayout) view
						.findViewById(R.id.biaoqian_1);
				for (int i = 0; i < RoofFragment.this.biaoqian_1.length; i++) {
					TextView textView = new TextView(getActivity());
					textView.setText(RoofFragment.this.biaoqian_1[i]);
					LayoutParams layoutParams = new LayoutParams(
							LayoutParams.WRAP_CONTENT,
							LayoutParams.WRAP_CONTENT);
					layoutParams.setMargins(5, 5, 5, 5);
					biaoqian_1.addView(textView, layoutParams);
				}
				FixGridLayout biaoqian_2 = (FixGridLayout) view
						.findViewById(R.id.biaoqian_2);
				for (int i = 0; i < RoofFragment.this.biaoqian_2.length; i++) {
					TextView textView = new TextView(getActivity());
					textView.setText(RoofFragment.this.biaoqian_2[i]);
					LayoutParams layoutParams = new LayoutParams(80, 50);
					layoutParams.setMargins(5, 5, 5, 5);
					biaoqian_2.addView(textView, layoutParams);
				}
				popupWindow.setOutsideTouchable(true);
				if (!isShow) {
					popupWindow.showAsDropDown(v);
					txt_more.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.up));
				} else {
					popupWindow.dismiss();
					txt_more.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.down));
				}
				isShow = !isShow;
			}
		});
		boPresenter = new LunBoPresenter(this);
		LunBoModel model = new LunBoModel();
		model.setPosition(6045);
		boPresenter.getInfo(model);

	}

	public class MyAdapter extends FragmentStatePagerAdapter {
		String[] _titles;

		public MyAdapter(FragmentManager fm, String[] titles) {
			super(fm);
			_titles = titles;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return _titles[position];
		}

		@Override
		public int getCount() {
			return _titles.length;
		}

		@Override
		public Fragment getItem(int position) {

			Fragment bfrag2 = null;
			if (position == 0) {
				bfrag2 = new HomPageItemFragment();
			} else {
				bfrag2 = new HomePageItemFragmentTypeTwo();
			}
			return bfrag2;
		}

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity model = super.handlerError(data);
		if (model != null) {
			tabs.setFillViewport(false);
			System.out.println(">>>>" + model.getData());
			try {
				HomePageModel arrayBanner = (HomePageModel) model.getData();
				titles = new String[arrayBanner.getCate().size() + 1];
				for (int i = 0; i < arrayBanner.getCate().size(); i++) {
					titles[i + 1] = arrayBanner.getCate().get(i).getName();
				}
				titles[0] = "首页";
				Log.d("hu", titles + "---title");
				pager.setAdapter(new MyAdapter(getChildFragmentManager(),
						titles));
				tabs.setViewPager(pager);
			} catch (Exception e) {
				// myHandler.sendEmptyMessage(GO_HOME);
				// try {
				// closeProgressDialog();
				// } catch (Exception e2) {
				// }
			}
			// try {
			// closeProgressDialog();
			// } catch (Exception e) {
			// // TODO: handle exception
			// }
		}
	}
}
